package fr.umlv.m2.jee.pmlv.service.subject.impl;

import fr.umlv.m2.jee.pmlv.persistence.subject.AbstractTheme;
import fr.umlv.m2.jee.pmlv.persistence.subject.dao.IAbstractThemeDAO;
import fr.umlv.m2.jee.pmlv.service.subject.DisplayAbstractTheme;
import fr.umlv.m2.jee.pmlv.service.subject.IDisplayAbstractThemeService;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Stephanie
 */
@Service("defautDisplayAbstractThemeService")
public class DefautDisplayAbstractThemeService implements IDisplayAbstractThemeService {


    @Autowired
    @Qualifier("hibernateAbstractThemeDAO")
    private IAbstractThemeDAO dao;

    @Override
    @Transactional(readOnly=true)
    public List<DisplayAbstractTheme> getAllDisplayAbstractTheme() {
        ArrayList< DisplayAbstractTheme > result = new ArrayList< DisplayAbstractTheme >(16);
        for(AbstractTheme AbstractTheme : dao.findAll() ) {
            result.add(new DisplayAbstractTheme(AbstractTheme));
        }
        Collections.sort(result, new Comparator<DisplayAbstractTheme>() {
            @Override
            public int compare(DisplayAbstractTheme o1, DisplayAbstractTheme o2) {
                return new Long(o1.getId()).compareTo( new Long(o2.getId()));
            }
        });
        return result;
    }

    @Override
    @Transactional(readOnly=true)
    public List<DisplayAbstractTheme> getAbstractThemeBySubjectIdAndByYear(long id, int year) {
        ArrayList< DisplayAbstractTheme > result = new ArrayList< DisplayAbstractTheme >(16);
        for(AbstractTheme AbstractTheme : dao.findBySubjectIdAndByYear(id, year) ) {
            result.add(new DisplayAbstractTheme(AbstractTheme));
        }
        Collections.sort(result, new Comparator<DisplayAbstractTheme>() {
            @Override
            public int compare(DisplayAbstractTheme o1, DisplayAbstractTheme o2) {
                return new Long(o1.getId()).compareTo( new Long(o2.getId()));
            }
        });
        return result;
    }

    @Override
    @Transactional(readOnly=true)
    public DisplayAbstractTheme getAbstractThemeById(long id) {
        DisplayAbstractTheme result = new DisplayAbstractTheme( dao.findById(id) );
        return result;
    }

    @Override
    @Transactional
    public void addAbstractTheme(DisplayAbstractTheme theme) {
    	dao.persist(theme.getAbstractTheme());
    }

    @Override
    @Transactional
    public void editAbstractTheme(DisplayAbstractTheme theme) {
    	dao.merge(theme.getAbstractTheme());
    }

    @Override
    @Transactional(readOnly=true)
    public void deleteAbstractTheme(long id) {
        AbstractTheme theme = dao.findById(id);
        dao.remove( theme );
    }

    @Override
    @Transactional(readOnly=true)
    public void publishAbstractTheme(long id) {
        AbstractTheme theme = dao.findById(id);
        theme.setPublished(1);
        dao.merge(theme);
    }
}
